package o;

import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

public final class fuu extends fuw {
    private static final byte[] ʻ = new byte[]{(byte) 58, (byte) 32};
    private static final byte[] ʼ = new byte[]{(byte) 45, (byte) 45};
    public static final fup ˊ = fup.ˎ("multipart/parallel");
    public static final fup ˋ = fup.ˎ(RequestBody.HEAD_VALUE_CONTENT_TYPE_FORM_DATA);
    public static final fup ˎ = fup.ˎ("multipart/mixed");
    public static final fup ˏ = fup.ˎ("multipart/alternative");
    public static final fup ॱ = fup.ˎ("multipart/digest");
    private static final byte[] ᐝ = new byte[]{(byte) 13, (byte) 10};
    private final fup ʽ;
    private long ˋॱ = -1;
    private final fup ͺ;
    private final List<d> ॱˊ;
    private final fxh ॱॱ;

    public static final class d {
        final fuw ˎ;
        @Nullable
        final fui ˏ;

        public static d ˊ(@Nullable fui o_fui, fuw o_fuw) {
            if (o_fuw == null) {
                throw new NullPointerException("body == null");
            } else if (o_fui != null && o_fui.ॱ(HttpContants.KEY_CONTENT_TYPE) != null) {
                throw new IllegalArgumentException("Unexpected header: Content-Type");
            } else if (o_fui == null || o_fui.ॱ("Content-Length") == null) {
                return new d(o_fui, o_fuw);
            } else {
                throw new IllegalArgumentException("Unexpected header: Content-Length");
            }
        }

        public static d ˊ(String str, String str2) {
            return ˋ(str, null, fuw.create(null, str2));
        }

        public static d ˋ(String str, @Nullable String str2, fuw o_fuw) {
            if (str == null) {
                throw new NullPointerException("name == null");
            }
            StringBuilder stringBuilder = new StringBuilder("form-data; name=");
            fuu.ˏ(stringBuilder, str);
            if (str2 != null) {
                stringBuilder.append("; filename=");
                fuu.ˏ(stringBuilder, str2);
            }
            return ˊ(fui.ˋ("Content-Disposition", stringBuilder.toString()), o_fuw);
        }

        private d(@Nullable fui o_fui, fuw o_fuw) {
            this.ˏ = o_fui;
            this.ˎ = o_fuw;
        }
    }

    public static final class e {
        private final fxh ˋ;
        private final List<d> ˏ;
        private fup ॱ;

        public e() {
            this(UUID.randomUUID().toString());
        }

        public e(String str) {
            this.ॱ = fuu.ˎ;
            this.ˏ = new ArrayList();
            this.ˋ = fxh.ˏ(str);
        }

        public e ˏ(fup o_fup) {
            if (o_fup == null) {
                throw new NullPointerException("type == null");
            } else if (o_fup.ˏ().equals("multipart")) {
                this.ॱ = o_fup;
                return this;
            } else {
                throw new IllegalArgumentException("multipart != " + o_fup);
            }
        }

        public e ˎ(@Nullable fui o_fui, fuw o_fuw) {
            return ॱ(d.ˊ(o_fui, o_fuw));
        }

        public e ॱ(String str, String str2) {
            return ॱ(d.ˊ(str, str2));
        }

        public e ˊ(String str, @Nullable String str2, fuw o_fuw) {
            return ॱ(d.ˋ(str, str2, o_fuw));
        }

        public e ॱ(d dVar) {
            if (dVar == null) {
                throw new NullPointerException("part == null");
            }
            this.ˏ.add(dVar);
            return this;
        }

        public fuu ˎ() {
            if (!this.ˏ.isEmpty()) {
                return new fuu(this.ˋ, this.ॱ, this.ˏ);
            }
            throw new IllegalStateException("Multipart body must have at least one part.");
        }
    }

    fuu(fxh o_fxh, fup o_fup, List<d> list) {
        this.ॱॱ = o_fxh;
        this.ʽ = o_fup;
        this.ͺ = fup.ˎ(o_fup + "; boundary=" + o_fxh.ˊ());
        this.ॱˊ = fvg.ˏ((List) list);
    }

    public fup contentType() {
        return this.ͺ;
    }

    public long contentLength() throws IOException {
        long j = this.ˋॱ;
        if (j != -1) {
            return j;
        }
        j = ˎ(null, true);
        this.ˋॱ = j;
        return j;
    }

    public void writeTo(fxj o_fxj) throws IOException {
        ˎ(o_fxj, false);
    }

    private long ˎ(@Nullable fxj o_fxj, boolean z) throws IOException {
        fxi o_fxi;
        long j = 0;
        if (z) {
            fxi o_fxi2 = new fxi();
            o_fxi = o_fxi2;
            o_fxj = o_fxi2;
        } else {
            o_fxi = null;
        }
        int size = this.ॱˊ.size();
        for (int i = 0; i < size; i++) {
            d dVar = (d) this.ॱˊ.get(i);
            fui o_fui = dVar.ˏ;
            fuw o_fuw = dVar.ˎ;
            o_fxj.ˊ(ʼ);
            o_fxj.ˊ(this.ॱॱ);
            o_fxj.ˊ(ᐝ);
            if (o_fui != null) {
                int ˋ = o_fui.ˋ();
                for (int i2 = 0; i2 < ˋ; i2++) {
                    o_fxj.ˋ(o_fui.ॱ(i2)).ˊ(ʻ).ˋ(o_fui.ˋ(i2)).ˊ(ᐝ);
                }
            }
            fup contentType = o_fuw.contentType();
            if (contentType != null) {
                o_fxj.ˋ("Content-Type: ").ˋ(contentType.toString()).ˊ(ᐝ);
            }
            long contentLength = o_fuw.contentLength();
            if (contentLength != -1) {
                o_fxj.ˋ("Content-Length: ").ˋॱ(contentLength).ˊ(ᐝ);
            } else if (z) {
                o_fxi.ʿ();
                return -1;
            }
            o_fxj.ˊ(ᐝ);
            if (z) {
                j += contentLength;
            } else {
                o_fuw.writeTo(o_fxj);
            }
            o_fxj.ˊ(ᐝ);
        }
        o_fxj.ˊ(ʼ);
        o_fxj.ˊ(this.ॱॱ);
        o_fxj.ˊ(ʼ);
        o_fxj.ˊ(ᐝ);
        if (!z) {
            return j;
        }
        j += o_fxi.ॱ();
        o_fxi.ʿ();
        return j;
    }

    static StringBuilder ˏ(StringBuilder stringBuilder, String str) {
        stringBuilder.append('\"');
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            switch (charAt) {
                case '\n':
                    stringBuilder.append("%0A");
                    break;
                case '\r':
                    stringBuilder.append("%0D");
                    break;
                case '\"':
                    stringBuilder.append("%22");
                    break;
                default:
                    stringBuilder.append(charAt);
                    break;
            }
        }
        stringBuilder.append('\"');
        return stringBuilder;
    }
}
